/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package parser.serverData.skillLearns;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import parser.clientData.clientSkills.SkillBaseClient;
import parser.clientData.clientStrings.ClientString;
import parser.clientData.skillLearns.ClientSkillLearn;
import parser.util.DataManager;

/**
 * @author Mr. Poke
 *
 */
public class SkillTreeSave
{

	public static void save()
	{
		Map<String, PlayerClass> className = new HashMap<String, PlayerClass>();
		className.put("warrior", PlayerClass.WARRIOR);
		className.put("fighter", PlayerClass.GLADIATOR);
		className.put("knight", PlayerClass.TEMPLAR);
		className.put("scout", PlayerClass.SCOUT);
		className.put("assassin", PlayerClass.ASSASSIN);
		className.put("ranger", PlayerClass.RANGER);
		className.put("mage", PlayerClass.MAGE);
		className.put("wizard", PlayerClass.SORCERER);
		className.put("elementallist", PlayerClass.SPIRIT_MASTER);
		className.put("cleric", PlayerClass.PRIEST);
		className.put("priest", PlayerClass.CLERIC);
		className.put("chanter", PlayerClass.CHANTER);
		className.put("all", PlayerClass.ALL);
		ObjectFactory objFactory = new ObjectFactory();
		SkillTree collection = (SkillTree) objFactory.createSkillTree();
		List<SkillTreeEntry> templateList = collection.getSkill();

		List<ClientSkillLearn> clientTree = DataManager.getInstance().getClientSkillLearn();
		for (ClientSkillLearn clientSkill : clientTree)
		{
			SkillTreeEntry tree = new SkillTreeEntry();
			tree.setMinLevel(clientSkill.getPcLevel());
			tree.setSkillLevel(clientSkill.getSkillLevel());
			PlayerClass clazz = className.get(clientSkill.getClazz().toLowerCase());
			if (clazz == null)
				System.out.println("PlayerClass missing: "+clientSkill.getClazz());
			else
				tree.setClassId(clazz);
			if (clientSkill.getStigmaDisplay() == 1)
				tree.setStigma(true);
			SkillBaseClient skill = DataManager.getInstance().getClientSkillsnameMap().get(clientSkill.getSkill().toUpperCase());
			tree.setSkillId(skill.getId());
			if (clientSkill.getRace().equalsIgnoreCase("ALL"))
				tree.setRace(Race.PC_ALL);
			else if (clientSkill.getRace().equalsIgnoreCase("PC_LIGHT"))
				tree.setRace(Race.ELYOS);
			else if (clientSkill.getRace().equalsIgnoreCase("PC_DARK"))
				tree.setRace(Race.ASMODIANS);
			else
				System.out.println("Missing race: "+clientSkill.getRace());
			ClientString name = DataManager.getInstance().getStringNameMap().get(skill.getDesc().toLowerCase());
			tree.setName(name.getBody());
			if (clientSkill.getAutolearn() != null && clientSkill.getAutolearn().equalsIgnoreCase("TRUE"))
				tree.setAutolearn(true);
			templateList.add(tree);
		}
		try
		{
			JAXBContext jaxbContext = JAXBContext.newInstance("parser.serverData.skillLearns");
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
			marshaller.marshal(collection, new FileOutputStream("static_data/skill_tree/skill_tree.xml"));
		}
		catch (PropertyException e)
		{
			e.printStackTrace();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (JAXBException e)
		{
			e.printStackTrace();
		}
	}
}
